catalogUrl = 'http://user:glos@64.9.200.121:8984/rest/glos';
xsl        = 'xsl/glos.iso.xsl';
xml        = 'xml/glosISOQuery.xml';

mapCenterAndZoom = {
   lon  : -84.4
  ,lat  : 44.0
  ,zoom : 6
};

function buildFilter(limit,start) {
  if (typeof limit != 'number') {
    limit = searchLimitPerPage;
  }
  if (typeof start != 'number') {
    start = searchStart;
  }

  var catalogQueryXML = <?php echo json_encode(file_get_contents('https://raw.github.com/asascience-open/glos_catalog/master/queries/full_search.xml'))?>;

  var filter = catalogQueryXML.replace('___LIMIT___',limit).replace('___START___',start);

  if (Ext.getCmp('anyTextSearchField').getValue() != '') {
    filter = filter.replace('___TEXTSEARCH___',1).replace('___ANYTEXT___',Ext.getCmp('anyTextSearchField').getValue());
  }
  else {
    filter = filter.replace('___TEXTSEARCH___',0).replace('___ANYTEXT___','');
  }

  var bbox = map.getExtent().transform(proj900913,proj4326).toArray();
  filter = filter.replace('___GEOSEARCH___',Ext.getCmp('buttonSyncWithMap').pressed ? 1 : 0).replace('___WEST___',bbox[0]).replace('___SOUTH___',bbox[1]).replace('___EAST___',bbox[2]).replace('___NORTH___',bbox[3]);

  // no temporal constraints yet
  filter = filter.replace('___TEMPORALSEARCH___',0);

  return filter;
}

Ext.override(Ext.PagingToolbar,{
  // override the following because start is 1-based instead of extjs's 0-based default
  moveFirst   : function() {
    this.doLoad(1);
  }
  ,onLoad : function(store, r, o){
    if(!this.rendered){
      this.dsLoaded = [store, r, o];
      return;
    }
    var p = this.getParams();
    this.cursor = (o.params && o.params[p.start]) ? o.params[p.start] : 1;
    var d = this.getPageData(), ap = d.activePage, ps = d.pages;

    this.afterTextItem.setText(String.format(this.afterPageText, d.pages));
    this.inputItem.setValue(ap);
    this.first.setDisabled(ap == 1);
    this.prev.setDisabled(ap == 1);
    this.next.setDisabled(ap == ps);
    this.last.setDisabled(ap == ps);
    this.refresh.enable();
    this.updateInfo();
    this.fireEvent('change', this, d);
  }
  ,updateInfo : function(){
    if(this.displayItem){
      var count = this.store.getCount();
      var msg = count == 0 ?
        this.emptyMsg :
        String.format(
          this.displayMsg,
          this.cursor, this.cursor+count - 1, this.store.getTotalCount()
        );
      this.displayItem.setText(msg);
    }
  }
  ,getPageData : function(){
    var total = this.store.getTotalCount();
    return {
      total : total,
      activePage : Math.ceil((this.cursor+this.pageSize - 1)/this.pageSize),
      pages :  total < this.pageSize ? 1 : Math.ceil(total/this.pageSize)
    };
  }
});
